using System;
using System.Collections;
using Gadgeteer.Interfaces;
using Gadgeteer.Interfaces.Led;

namespace Gadgeteer.Interfaces.Sample.PhotoCamera.CameraApp
{
    public class BusyIndicationStack
    {
        private readonly ArrayList items;
        private readonly IMultiColorLed statusLed;

        public BusyIndicationStack(IMultiColorLed statusLed)
        {
            items = new ArrayList();
            this.statusLed = statusLed;
            statusLed.TurnGreen();
        }

        public bool Busy
        {
            get { return items.Count != 0; }
        }

        public BusyIndicator Get()
        {
            var indicator = new BusyIndicator(this);
            items.Add(indicator);
            statusLed.TurnRed();
            return indicator;
        }

        internal void Remove(BusyIndicator busyIndicator)
        {
            items.Remove(busyIndicator);
            if (items.Count == 0)
            {
                statusLed.TurnGreen();
            }
        }
    }
}
